package com.ruge.system.service.impl;

import com.ruge.common.properties.RugeMenuProperties;
import com.ruge.common.util.Constants;
import com.ruge.common.util.ShiroUtil;
import com.ruge.common.util.TreeUtil;
import com.ruge.system.pojo.entity.SysMenu;
import com.ruge.system.pojo.entity.SysUser;
import com.ruge.system.service.SysLoginService;
import com.ruge.system.service.SysMenuService;
import com.ruge.system.pojo.vo.ActiveUser;
import com.ruge.system.pojo.vo.MenuVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ruge.wu
 * @program ruge
 * @description 登录逻辑实现层
 * @date 2020-03-10 11:17
 **/
@Service
public class SysLoginServiceImpl implements SysLoginService {

    @Resource
    private SysMenuService sysMenuService;
    @Resource
    private RugeMenuProperties rugeProperties;

    /**
     * 登录加载菜单
     *
     * @return 菜单
     */
    @Override
    public Map<String, Object> menu() {
        Map<String, Object> map = new HashMap<>(16);
        List<SysMenu> menuList = new ArrayList<>();
        List<MenuVo> menuInfo = new ArrayList<>();
        ActiveUser activeUser= ShiroUtil.getActiveUser();
        SysUser sysUser = activeUser.getUser();
        if (Constants.USER_TYPE_SUPER.equals(sysUser.getType())) {
            menuList = sysMenuService.findAll();
        } else if (Constants.USER_TYPE_NORMAL.equals(sysUser.getType())) {
            menuList = new ArrayList<>(activeUser.getPermissions());
        }
        for (SysMenu e : menuList) {
            MenuVo menuVO = new MenuVo();
            menuVO.setId(e.getId());
            menuVO.setPid(e.getPid());
            menuVO.setHref(e.getHref());
            menuVO.setTitle(e.getTitle());
            menuVO.setIcon(e.getIcon());
            menuVO.setTarget(e.getTarget());
            menuInfo.add(menuVO);
        }
        map.put("menuInfo", TreeUtil.toTree(menuInfo, 0L));
        rugeProperties.getMenu().forEach(e -> {
            map.putAll(e);
        });
        return map;
    }

}
